/*
 * Copyright (C) 2011 CrystalRange
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.crystalrange.enterprise.web;

import com.googlecode.genericdao.search.Search;
import java.util.List;
import java.util.Set;
import org.crystalrange.BaseController;
import org.crystalrange.enterprise.model.StorageUnitType;
import org.crystalrange.enterprise.service.StorageUnitTypeService;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zul.Listbox;
import org.zkoss.zul.Listitem;
import org.zkoss.zul.Messagebox;
import org.zkoss.zul.Window;

public class StorageUnitTypeListController extends BaseController {

    protected StorageUnitTypeService storageUnitTypeService;
    protected StorageUnitType selectedStorageUnitType;
    protected Listbox listboxStorageUnitType;
    protected Search searchStorageUnitType;
    protected List<StorageUnitType> storageUnitTypes;

    public StorageUnitTypeService getStorageUnitTypeService() {
        return this.storageUnitTypeService;
    }

    public void setStorageUnitTypeService(StorageUnitTypeService storageUnitTypeService) {
        this.storageUnitTypeService = storageUnitTypeService;
    }

    public StorageUnitType getSelectedStorageUnitType() {
        return this.selectedStorageUnitType;
    }

    public void setSelectedStorageUnitType(StorageUnitType selectedStorageUnitType) {
        this.selectedStorageUnitType = selectedStorageUnitType;
    }

    public List<StorageUnitType> getStorageUnitTypes() {
        return this.storageUnitTypes;
    }

    @Override
    public void doAfterCompose(Component component) throws Exception {
        super.doAfterCompose(component);

        this.searchStorageUnitType = new Search(StorageUnitType.class);
        this.searchStorageUnitType.setMaxResults(100);
        this.searchStorageUnitType.addFetch("warehouseNumber");
        this.searchStorageUnitType.addFetch("createdBy");
        this.searchStorageUnitType.addFetch("updatedBy");
        this.storageUnitTypes = this.storageUnitTypeService.search(this.searchStorageUnitType);

        component.setAttribute("controller", this);
    }

    public void onClick$menuitemStorageUnitTypeCreate(Event event) throws Exception {
        this.storageUnitTypeCreate();
    }

    public void onClick$menuitemStorageUnitTypeEdit(Event event) throws Exception {
        this.storageUnitTypeEdit();
    }

    public void onClick$menuitemStorageUnitTypeDelete(Event event) throws Exception {
        this.storageUnitTypeDelete();
    }

    public void onClick$menuitemStorageUnitTypeSearch(Event event) throws Exception {
        this.storageUnitTypeSearch();
    }

    public void onClick$menuitemStorageUnitTypeRefresh(Event event) throws Exception {
        this.storageUnitTypeRefresh();
    }

    public void onClick$toolbarbuttonStorageUnitTypeCreate(Event event) throws Exception {
        this.storageUnitTypeCreate();
    }

    public void onClick$toolbarbuttonStorageUnitTypeEdit(Event event) throws Exception {
        this.storageUnitTypeEdit();
    }

    public void onClick$toolbarbuttonStorageUnitTypeDelete(Event event) throws Exception {
        this.storageUnitTypeDelete();
    }

    public void onClick$toolbarbuttonStorageUnitTypeSearch(Event event) throws Exception {
        this.storageUnitTypeSearch();
    }

    public void onClick$toolbarbuttonStorageUnitTypeRefresh(Event event) throws Exception {
        this.storageUnitTypeRefresh();
    }

    public void onDoubleClickListItem(Listitem listitem) throws Exception {
        this.storageUnitTypeEdit();
    }
    
    private void storageUnitTypeCreate() throws Exception {
        this.execution.sendRedirect("~./zul/html/storageUnitType.zul");
    }

    private void storageUnitTypeEdit() throws Exception {
        if (this.selectedStorageUnitType != null) {
            this.execution.sendRedirect("~./zul/html/storageUnitType.zul?Id=" + this.selectedStorageUnitType.getId().toString());
        }
    }

    private void storageUnitTypeDelete() throws Exception {
        Set<Listitem> selectedItems = this.listboxStorageUnitType.getSelectedItems();

        if (selectedItems.size() > 0) {
            if (Messagebox.YES == Messagebox.show("Delete these records?", "Delete?",
                    Messagebox.YES | Messagebox.NO, Messagebox.QUESTION)) {
                for (Listitem selectedItem : selectedItems) {
                    StorageUnitType storageUnitType = (StorageUnitType) selectedItem.getValue();
                    this.storageUnitTypeService.remove(storageUnitType);
                }
            }
        } else {
            this.message.setText("Please select at least one item!");
            this.message.setIcon("/img/exclamation.png");
        }

        this.storageUnitTypes = this.storageUnitTypeService.search(this.searchStorageUnitType);
        this.binder.loadAll();
    }

    private void storageUnitTypeSearch() throws Exception {
        this.execution.setAttribute("searchStorageUnitType", this.searchStorageUnitType);

        Window window = (Window) this.execution.createComponents("~./zul/html/storageUnitTypeSearch.zul", null, null);
        window.setParent(self);
        window.doModal();

        if (this.execution.getAttribute("result") == "ok") {
            this.searchStorageUnitType = (Search) this.execution.getAttribute("searchStorageUnitType");
        }

        this.storageUnitTypes = this.storageUnitTypeService.search(this.searchStorageUnitType);
        this.binder.loadAll();
    }

    private void storageUnitTypeRefresh() throws Exception {
        this.storageUnitTypes = this.storageUnitTypeService.search(this.searchStorageUnitType);
        this.binder.loadAll();
    }
}
